package com.tledu.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * @author cyrus
 */
@Aspect
@Component
public class MyAspect2 {

    @Pointcut("execution(* com.tledu.*.IUser*.select*(..))")
    public void pointCut() {

    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        // 执行方法之前
        System.out.println("around前....");
        Long beginTime = System.currentTimeMillis();

        // 调用要执行的方法
//        joinPoint.getArgs();
        Object result = joinPoint.proceed();
        System.out.println("方法返回值：" + result);
        // 执行方法之后
        System.out.println("执行了多少毫秒:" + (System.currentTimeMillis() - beginTime));
        System.out.println("around后....");

        System.out.println("-------------------------------------");
        return result;
    }

    @Before(value = "pointCut()")
    public void before(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        System.out.println("参数如下：");
        for (int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
        // 获取原始对象的类
        System.out.println("==========================================");
        // 1. 获取类信息
        Class clazz = joinPoint.getTarget().getClass();
        System.out.println("类名：" + clazz.getSimpleName());
        System.out.println("全类名：" + clazz.getName());
        // 2. 获取要执行的方法信息
        System.out.println("方法名:" + joinPoint.getSignature().getName());
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        // 获取执行的方法反射对象
        Method method = methodSignature.getMethod();
        // 获取执行方法的注解
        Annotation[] annoArr = method.getAnnotations();
        System.out.println("方法名：" + method.getName());
        System.out.println("==========================================");
        System.out.println("before...");
    }

    @AfterReturning(value = "pointCut()", returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result) {
        Object[] args = joinPoint.getArgs();
        System.out.println("参数如下：");
        for (int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
        System.out.println("AfterReturning获取返回值：" + result);
        System.out.println("AfterReturning...");
    }

    @AfterThrowing(value = "pointCut()", throwing = "e")
    public void afterThrowing(JoinPoint joinPoint, Exception e) {
        Object[] args = joinPoint.getArgs();
        System.out.println("参数如下：");
        for (int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
        System.out.println("afterThrowing，异常msg:" + e.getMessage());
        System.out.println("afterThrowing...");
    }

    @After("pointCut()")
    public void customFuncName(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        System.out.println("参数如下：");
        for (int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
        System.out.println("after。。。");
    }
}
